﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Reflection;
using Kawanon.Localization;

namespace Kawanon.QtHoney
{
	public partial class CollectionEditorForm : Form
	{
		public CollectionEditorForm()
		{
			InitializeComponent();

			StringResource.Apply(MessageBoxButtons.OKCancel, buttonOk, buttonCancel);
			StringResource.Apply("CollectionEditorForm_ToolBar", toolStrip1.Items);

			this.Text = StringResource.Localize(MiscResources.MyCollectionEditor_DefaultTitle);
		}

		private IList m_source = null;

		public Type ItemType { get; private set; }
		public IList List
		{
			get
			{
				IList list = (IList)Activator.CreateInstance(m_source.GetType());
				foreach (object obj in listBox1.Items)
				{
					list.Add(obj);
				}
				return list;
			}
			set
			{
				m_source = value;

				listBox1.Items.Clear();
				foreach (object obj in m_source)
				{
					listBox1.Items.Add(obj);
				}
				ResolveItemType();
				UpdateToolbarButtons();
			}
		}

		// CollectionEditor と同じやり方でアイテムの型を決定
		private void ResolveItemType()
		{
			PropertyInfo prop = m_source.GetType().GetProperty("Item", BindingFlags.Instance | BindingFlags.Public);
			ItemType = prop.PropertyType;
		}

		private void UpdateToolbarButtons()
		{
			bool noSelection = (listBox1.SelectedIndex == -1);

			ToolStripButton[] buttons = new ToolStripButton[] {
				toolStripButton_Remove, toolStripButton_Up, toolStripButton_Down };
			Array.ForEach(buttons, (button) => button.Enabled = !noSelection);
		}

		private void CollectionEditorForm_Load(object sender, EventArgs e)
		{
			propertyGrid1.ContextMenuStrip = Utility.CreateContextMenuStrip(propertyGrid1);

			if (listBox1.Items.Count > 0)
			{
				listBox1.SelectedIndex = 0;
			}
			UpdateToolbarButtons();
		}

		private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			UpdateToolbarButtons();

			if (listBox1.SelectedIndex == -1)
			{
				return;
			}
			// 要素が値型だとうまく動かないかも
			object obj = listBox1.Items[listBox1.SelectedIndex];
			propertyGrid1.SelectedObject = obj;
		}

		private void toolStripButton_Add_Click(object sender, EventArgs e)
		{
			object obj = Activator.CreateInstance(ItemType);
			listBox1.SelectedIndex = listBox1.Items.Add(obj);
		}

		private void toolStripButton_Remove_Click(object sender, EventArgs e)
		{
			int index = listBox1.SelectedIndex;
			listBox1.Items.Remove(listBox1.Items[index]);

			if (listBox1.Items.Count > index)
			{
				listBox1.SelectedIndex = index;
			}
			else if (listBox1.Items.Count == index)
			{
				listBox1.SelectedIndex = index - 1;
			}
		}

		private void UpDown(bool up)
		{
			int index = listBox1.SelectedIndex;
			object obj = listBox1.Items[index];

			bool flip = up ? (index == 0) : (index == listBox1.Items.Count - 1);
			listBox1.Items.Remove(obj);

			int newIndex;
			if (up)
			{
				newIndex = flip ? listBox1.Items.Count : index - 1;
			}
			else
			{
				newIndex = flip ? 0 : index + 1;
			}
			listBox1.Items.Insert(newIndex, obj);
			listBox1.SelectedIndex = newIndex;
		}

		private void toolStripButton_Up_Click(object sender, EventArgs e)
		{
			UpDown(true);
		}

		private void toolStripButton_Down_Click(object sender, EventArgs e)
		{
			UpDown(false);
		}

		private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
		{
			int index = listBox1.SelectedIndex;
			Reflection.InvokeMethod(listBox1, "RefreshItems", null);
			listBox1.SelectedIndex = index;
		}
	}
}
